# *-* coding=utf-8 *-*
# 人脸识别WebService

import base64
import tempfile

from flask import Flask, request, render_template

from face_service import *

app = Flask(__name__)


# 获取图片中人脸数量
@app.route('/face_num', methods=['GET', 'POST'])
def face_num():
    # 判断请求类型
    if request.method == 'POST':

        if "file_date" in request.form:
            if request.form['file_date']:
                image_data = request.form['file_date']
                temp = tempfile.TemporaryFile()
                image_bytes = base64.b64decode(image_data)
                temp.write(image_bytes)
                return str(get_faces_num(temp))

    return "0"


# 获取图片中人脸坐标
@app.route('/point', methods=['GET', 'POST'])
def face_point():
    # 判断请求类型
    if request.method == 'POST':

        if "file_date" in request.form:
            if request.form['file_date']:
                image_data = request.form['file_date']
                temp = tempfile.TemporaryFile()
                image_bytes = base64.b64decode(image_data)
                temp.write(image_bytes)
                return get_faces_points(temp)
    return ""


# 获取人脸68个特征点
@app.route('/landmark', methods=['GET', 'POST'])
def face_landmark():
    # 判断请求类型
    if request.method == 'POST':

        if "file_date" in request.form:
            if request.form['file_date']:
                image_data = request.form['file_date']
                temp = tempfile.TemporaryFile()
                image_bytes = base64.b64decode(image_data)
                temp.write(image_bytes)
                return get_face_landmark(temp)

    return ""


# 获取人脸128维嵌入向量
@app.route('/encode', methods=['GET', 'POST'])
def face_encode():
    # 判断请求类型
    if request.method == 'POST':
        if "file_date" in request.form:
            if request.form['file_date']:
                image_data = request.form['file_date']
                temp = tempfile.TemporaryFile()
                image_bytes = base64.b64decode(image_data)
                temp.write(image_bytes)
                # save_local(image_bytes)
                return get_face_encode(temp)

    return ""


# 比较人脸相似度
@app.route('/compare', methods=['GET', 'POST'])
def face_compare():
    # 判断请求类型
    if request.method == 'POST':

        if "file_data1" in request.form:
            if request.form["file_data1"] and request.form["file_data2"]:
                image_data1 = request.form['file_data1']
                image_data2 = request.form['file_data2']
                temp1 = tempfile.TemporaryFile()
                temp2 = tempfile.TemporaryFile()
                temp1.write(base64.b64decode(image_data1))
                temp2.write(base64.b64decode(image_data2))
                return get_face_sim(temp1, temp2)

    return ""


# 类型限制
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def index():
    # 判断请求类型
    return render_template('index.html')


@app.route('/register', methods=['GET', 'POST'])
def face_register():
    # 判断请求类型
    if request.method == 'POST':

        if "file_date" in request.form and "name" in request.form:
            if request.form['file_date']:
                image_data = request.form['file_date']
                temp = tempfile.TemporaryFile()
                image_bytes = base64.b64decode(image_data)
                temp.write(image_bytes)
                name = request.form["name"]
                face_regist(temp, name)
                return "success"

    return render_template('register.html')


# identify
@app.route('/identify', methods=['GET', 'POST'])
def face_identify():
    # 判断请求类型
    if request.method == 'POST':

        if "file_date" in request.form:
            if request.form['file_date']:
                image_data = request.form['file_date']
                temp = tempfile.TemporaryFile()
                image_bytes = base64.b64decode(image_data)
                temp.write(image_bytes)
                return get_face_name(temp)

    return render_template('identify.html')


# 测试读取身份证信息
@app.route('/test', methods=['GET', 'POST'])
def face_idcard():
    return render_template('test.html')



if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=False)
